
package logical_spaces.beans;

import Entity.TableSpace;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import logical_spaces.business.LogicalSpaceBusiness;
import logical_spaces.business.TableSpaceBusiness;
import logical_spaces.business.TableSpaceDataModel;
import login.beans.ConectionBean;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean
@RequestScoped

public class DeleteTableSpaceBeanPermanent implements Serializable {

    private List<TableSpace> tableSpaces;
    private TableSpaceDataModel mediumTableSpaceModel;
    private static final long serialVersionUID = 308L;
    public TableSpace selectedtablespace;
    private Connection conn;

    public DeleteTableSpaceBeanPermanent(  ) {
        
        FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean =(ConectionBean) session.getAttribute("conectionBean");        
        this.conn = conectionBean.currentConnection;
        
        tableSpaces = new ArrayList<>();
        chargeDataTableSpaces();
        mediumTableSpaceModel = new TableSpaceDataModel(tableSpaces);
                
    }

    public List<TableSpace> getTableSpaces() {
        return tableSpaces;
    }

    public void setTableSpaces(List<TableSpace> tableSpaces) {
        this.tableSpaces = tableSpaces;
    }

    public TableSpaceDataModel getMediumTableSpaceModel() {
        return mediumTableSpaceModel;
    }

    public void setMediumTableSpaceModel(TableSpaceDataModel mediumTableSpaceModel) {
        this.mediumTableSpaceModel = mediumTableSpaceModel;
    }

    public TableSpace getSelectedtablespace() {
        return selectedtablespace;
    }

    public void setSelectedtablespace(TableSpace selectedtablespace) {
        this.selectedtablespace = selectedtablespace;
    }
    
    private void chargeDataTableSpaces() {
        LogicalSpaceBusiness lsb;
        try {
            lsb = new LogicalSpaceBusiness( conn );
            tableSpaces = lsb.chargePermanentTableSpaceData();
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN,
                            "Error de Consulta!",
                            "Please Try Again! " + ex.getMessage()));
        }
    }

    public void drop(){
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null,new FacesMessage("Advertencia",new TableSpaceBusiness( conn ).dropTablespacesQuery(selectedtablespace.getName())));
        chargeDataTableSpaces();
        mediumTableSpaceModel = new TableSpaceDataModel(tableSpaces);
    }
   
}
